package com.chzc.sys.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.chzc.po.Menu;
import com.chzc.sys.req.MenuREQ;
import com.chzc.sys.service.IMenuService;
import com.chzc.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 菜单信息表 前端控制器
 * </p>
 *
 * @author CV大魔王
 * @since 2021-05-24
 */
@Api(value = "菜单管理接口(私有)", tags = "菜单管理接口(私有)")
@RestController
@RequestMapping("/pri/menu")
public class MenuController {


    @Autowired
    private IMenuService menuService;

    @ApiOperation("根据菜单名称查询列表接口")
    @PostMapping("/search")
    public List<Menu> search(@RequestBody MenuREQ req) {
        return menuService.queryList(req);
    }


    @ApiOperation("通过菜单ID删除菜单接口")
    @ApiImplicitParam(name="id", value="菜单ID", required=true)
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id) {
        return menuService.deleteById(id);
    }


    @ApiOperation("新增菜单接口")
    @PostMapping // 请求地址 /menu
    public Result save(@RequestBody Menu menu) {
        menuService.saveMenu(menu);
        return Result.buildSuccess();
    }


    @ApiOperation("查询菜单接口")
    @ApiImplicitParam(name="id", value="菜单ID", required=true)
    @GetMapping("/{id}")
    public Result view(@PathVariable("id") String id){
        return Result.buildSuccess(menuService.getById(id));
    }


    @ApiOperation("修改菜单接口")
    @PutMapping
    public Result update(@RequestBody Menu menu) {
        // 更新时间
        menu.setUpdateDate(new Date());
        menuService.updateMenuById(menu);
        return Result.buildSuccess();
    }


    @ApiOperation("通过用户ID查询拥有的权限菜单树")
    @ApiImplicitParam(name="userId", value="用户ID", required=true)
    @GetMapping("/user/{userId}")
    public Result findUserMenuTree(@PathVariable("userId") String userId) {
        return menuService.findUserMenuTree(userId);
    }

    @ApiOperation("获取自己拥有的权限菜单树")
    @GetMapping("/me/tree")
    public Result findMeMenuTree() {
        String userId = (String) StpUtil.getLoginId();
        return menuService.findUserMenuTree(userId);
    }
}
